/*
 * File Name：CWPopMenu.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： CWPopMenu.java
 * Modify By：res-jxliu
 * Modify Date：2015年3月23日
 * Modify Type：Add
 */
package com.ciwong.mobilelib.widget;

import java.util.List;

import com.ciwong.mobilelib.R;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

/**
 * PopupWindow下拉菜单
 * 
 * @author res-jxliu
 * @version ciwong v.1.0 2015年3月23日
 * @since ciwong v.1.0
 */
public class CWPopMenu
{
    private final int xoff = 0;

    private final int yoff = 10;

    private PopupWindow mPopupWindow;

    private ListView mLVPopMenu;

    private View v;

    private boolean isCenter = false;

    /**
     * PopupWindow菜单
     * 
     * @param context
     *            上下文
     * @param popMenuInfos
     *            ListView填充数据数组
     */
    public CWPopMenu(Context context, List<PopMenuInfo> popMenuInfos)
    {
        v = View.inflate(context, R.layout.popmenu, null);
        // 设置 listview
        mLVPopMenu = (ListView) v.findViewById(R.id.popMenuLV);
        mLVPopMenu.setAdapter(new PopMenuAdapter(context, popMenuInfos));
        mPopupWindow = new PopupWindow(v);
        // 使其聚集
        mPopupWindow.setFocusable(true);
        // 设置允许在外点击消失
        mPopupWindow.setOutsideTouchable(true);
        // mPopupWindow.setBackgroundDrawable(context.getResources().getDrawable(
        // R.drawable.popmenu_bg));
        setPopupWindowBackgroundDrawable(context, R.mipmap.popmenu_bg);
    }

    /**
     * PopupWindow菜单
     * 
     * @param context
     *            上下文
     * @param popMenuInfos
     *            ListView填充数据数组
     */
    public CWPopMenu(Context context, List<PopMenuInfo> popMenuInfos, int color,
            boolean ismyview)
    {
        v = View.inflate(context, R.layout.popmenu, null);
        // 设置 listview
        mLVPopMenu = (ListView) v.findViewById(R.id.popMenuLV);
        mLVPopMenu.setAdapter(
                new PopMenuAdapter(context, popMenuInfos, color, ismyview));
        mPopupWindow = new PopupWindow(v, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        // 使其聚集
        mPopupWindow.setFocusable(true);
        // 设置允许在外点击消失
        mPopupWindow.setOutsideTouchable(true);
        // mPopupWindow.setBackgroundDrawable(context.getResources().getDrawable(
        // R.drawable.popmenu_bg));
        setPopupWindowBackgroundDrawable(context, R.mipmap.popmenu_bg);
    }

    /**
     * 
     * 将文字设置到TextView中心
     * 
     * @param flag
     */
    public void setTextCenter(boolean flag)
    {
        isCenter = true;
    }

    /**
     * 设置PopupWindow背景
     * 
     * @param context
     *            上下文
     * @param drawableId
     *            背景图
     */
    public void setPopupWindowBackgroundDrawable(Context context,
            int drawableId)
    {
        mPopupWindow.setBackgroundDrawable(
                context.getResources().getDrawable(drawableId));
    }

    /**
     * 设置菜单项点击监听器
     * 
     * @param listener
     *            菜单项点击监听器
     */
    public void setOnItemClickListener(
            android.widget.AdapterView.OnItemClickListener listener)
    {
        mLVPopMenu.setOnItemClickListener(listener);
    }

    /**
     * 监听popupWindow窗口关闭
     * 
     * @param listener
     *            窗口关闭听器
     */
    public void setOnDismissListener(PopupWindow.OnDismissListener listener)
    {
        mPopupWindow.setOnDismissListener(listener);
    }

    /**
     * 下拉式 弹出 pop菜单 parent
     * 
     * @param parent
     *            父容器
     * @param width
     *            popwindow 宽
     * @param height
     *            popwindow高
     */
    public void showAsDropDown(View parent, int width, int height)
    {
        // 保证尺寸是根据屏幕像素密度来的
        mPopupWindow.showAsDropDown(parent, xoff, yoff);
        mPopupWindow.update(width, height);
    }

    /**
     * 下拉式 弹出 pop菜单 parent
     * 
     * @param parent
     *            父容器
     * @param xoffParams
     *            popwindow x轴偏移量
     * @param yoffParams
     *            popwindow y轴偏移量
     * @param width
     *            popwindow 宽
     * @param height
     *            popwindow高
     */
    public void showAsDropDown(View parent, int xoffParams, int yoffParams,
            int width, int height)
    {
        // 保证尺寸是根据屏幕像素密度来的
        mPopupWindow.showAsDropDown(parent, xoffParams, yoffParams);
        mPopupWindow.update(width, height);
    }

    /**
     * 在view上方居中弹出
     * 
     * @param parent
     * @param gravity
     * @param xoffParams
     * @param yoffParams
     * @param width
     * @param height
     */
    public void showAtLocation(View parent, int gravity, int xoffParams,
            int yoffParams, int width, int height)
    {
        mPopupWindow.showAtLocation(parent, gravity, xoffParams, yoffParams);
        mPopupWindow.update(width, height);
    }

    /**
     * 获取高度
     * 
     * @return
     */
    public int getHeight()
    {
        return mPopupWindow.getHeight();
    }

    /**
     * 下拉式 弹出 pop菜单 parent
     * 
     * @param parent
     *            父容器
     */
    public void showAsDropDownCenter(View parent)
    {
        // 保证尺寸是根据屏幕像素密度来的
        mPopupWindow.showAsDropDown(parent, -parent.getWidth() * 3 / 2, 0);
        mPopupWindow.update(parent.getWidth() * 4, LayoutParams.WRAP_CONTENT);
    }

    /**
     * 下拉式 弹出 pop菜单 parent
     * 
     * @param parent
     *            父容器
     */
    public void showAsDropDown(View parent)
    {
        // 保证尺寸是根据屏幕像素密度来的
        mPopupWindow.showAsDropDown(parent, xoff, yoff);
        mPopupWindow.update(parent.getWidth() * 3, LayoutParams.WRAP_CONTENT);
    }

    /**
     * 隐藏菜单
     * 
     */
    public void dismiss()
    {
        mPopupWindow.dismiss();
    }

    /**
     * 菜单适配器
     * 
     */
    private final class PopMenuAdapter extends BaseAdapter
    {
        private Context mContext;

        private List<PopMenuInfo> mPopMenuInfos;

        private int color;

        /**
         * 
         * 是否自定义convertview
         */
        private boolean iscustomview = false;

        private LayoutParams params;

        /**
         * 
         * 构造函数
         * 
         * @param context
         *            上下文对象
         * @param popMenuInfos
         *            数据的集合
         */
        public PopMenuAdapter(Context context, List<PopMenuInfo> popMenuInfos)
        {
            this.mContext = context;
            this.mPopMenuInfos = popMenuInfos;
        }

        /**
         * 
         * 构造函数
         * 
         * @param context
         *            上下文对象
         * @param popMenuInfos
         *            数据的集合
         */
        public PopMenuAdapter(Context context, List<PopMenuInfo> popMenuInfos,
                int color, boolean iscustomview)
        {
            this.mContext = context;
            this.mPopMenuInfos = popMenuInfos;
            this.color = color;
            this.iscustomview = iscustomview;
        }

        @Override
        public int getCount()
        {
            return mPopMenuInfos == null ? 0 : mPopMenuInfos.size();
        }

        @Override
        public Object getItem(int position)
        {
            return mPopMenuInfos == null || position >= mPopMenuInfos.size()
                    ? null : mPopMenuInfos.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View view = null;
            if (convertView == null)
            {
                view = View.inflate(mContext, R.layout.popmenu_item, null);
            }
            else
            {
                view = convertView;
            }
            final PopMenuInfo popMenuInfo = mPopMenuInfos.get(position);
            TextView tv = ((TextView) view.findViewById(R.id.tv_popmenu));
            tv.setText(popMenuInfo.getPopMenuName());
            tv.setCompoundDrawablesWithIntrinsicBounds(
                    popMenuInfo.getPopMenuImg(), 0, 0, 0);
            if (isCenter)
            {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                tv.setLayoutParams(params);
                tv.setGravity(Gravity.CENTER);
            }
            if (iscustomview)
            {
                tv.setTextColor(color);
                //
                // LinearLayout.LayoutParams ltlp = new
                // LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                // LayoutParams.WRAP_CONTENT);
                // ltlp.gravity = Gravity.CENTER;
                // view.setLayoutParams(ltlp);
            }

            return view;
        }
    }

    /**
     * 菜单实体
     */
    public static class PopMenuInfo
    {
        private int popMenuImg;

        private String popMenuName;

        public int getPopMenuImg()
        {
            return popMenuImg;
        }

        public void setPopMenuImg(int popMenuImg)
        {
            this.popMenuImg = popMenuImg;
        }

        public String getPopMenuName()
        {
            return popMenuName;
        }

        public void setPopMenuName(String popMenuName)
        {
            this.popMenuName = popMenuName;
        }
    }

}
